package com.shop.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 *
 * @author ZJL
 * @date 2025/6/3
 */
@PropertySource("classpath:alipay.properties")
@Component
public class AlipayUtils implements InitializingBean {
    /**
     * 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
     */
    @Value("${appId}")
    private String appId;
    /**
     * 应用私钥，就是工具生成的应用私钥
     */
    @Value("${merchantPrivateKey}")
    private String merchantPrivateKey;
    /**
     * 支付宝公钥,对应APPID下的支付宝公钥
     */
    @Value("${alipayPublicKey}")
    public String alipayPublicKey;
    /**
     * 异步通知，支付成功之后，支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
     */
    @Value("${notifyUrl}")
    public String notifyUrl;
    /**
     * 同步通知，支付成功之后，支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
     */
    @Value("${returnUrl}")
    public String returnUrl;
    /**
     * 签名方式
     */
    @Value("${signType}")
    private String signType;
    /**
     * 字符编码格式
     */
    @Value("${charset}")
    private String charset;
    /**
     * 订单超时时间
     */
    private String timeout = "10m";
    /**
     * 支付宝网关
     */
    @Value("${gatewayUrl}")
    private String gatewayUrl;
    private AlipayClient alipayClient = null;


    @Override
    public void afterPropertiesSet() throws Exception {
        // 根据支付宝的配置生成一个支付客户端
        alipayClient = new DefaultAlipayClient(gatewayUrl, appId, merchantPrivateKey, "json", charset, alipayPublicKey, signType);
    }

    /**
     * 支付宝APP支付
     *
     * @return 支付参数
     */
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        // 创建一个APP支付请求
        AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
        alipayRequest.setNotifyUrl(notifyUrl);

        // 使用JSONObject安全构建参数
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
        bizContent.put("total_amount", totalAmount.toString());
        bizContent.put("subject", subject);
        bizContent.put("product_code", "QUICK_MSECURITY_PAY");
        bizContent.put("timeout_express", timeout);

        alipayRequest.setBizContent(bizContent.toString());

        // 获取支付参数
        return alipayClient.sdkExecute(alipayRequest).getBody();
    }

    /**
     * 通过订单编号查询支付信息
     *
     * @param id 订单编号
     * @return 支付信息
     */
    public String queryOrder(String id) {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());

        AlipayTradeQueryResponse response = null;
        String body = null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException("调用支付宝查询订单接口异常",e);
        }
        return body;
    }

}
